package com.ruoyi.web.controller.gatewayadmin;

import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gatewayadmin.domain.GatewayAppRouter;
import com.ruoyi.gatewayadmin.service.IGatewayAppRouterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 应用权限Controller
 *
 * @author Darwin
 * @date 2024-10-14
 */
@RestController
@RequestMapping("/gatewayadmin/permission")
public class GatewayAppRouterController extends BaseController {
    @Autowired
    private IGatewayAppRouterService gatewayAppRouterService;

    /**
     * 查询应用权限列表
     */
    @PreAuthorize("@ss.hasPermi('gatewayadmin:permission:list')")
    @GetMapping("/list")
    public TableDataInfo list(GatewayAppRouter gatewayAppRouter) {
        startPage();
        List<GatewayAppRouter> list = gatewayAppRouterService.selectGatewayAppRouterList(gatewayAppRouter);
        return getDataTable(list);
    }

    /**
     * 导出应用权限列表
     */
    @PreAuthorize("@ss.hasPermi('gatewayadmin:permission:export')")
    @Log(title = "应用权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GatewayAppRouter gatewayAppRouter) {
        List<GatewayAppRouter> list = gatewayAppRouterService.selectGatewayAppRouterList(gatewayAppRouter);
        ExcelUtil<GatewayAppRouter> util = new ExcelUtil<GatewayAppRouter>(GatewayAppRouter.class);
        util.exportExcel(response, list, "应用权限数据");
    }

    /**
     * 获取应用权限详细信息
     */
    @PreAuthorize("@ss.hasPermi('gatewayadmin:permission:query')")
    @GetMapping(value = "/{uuid}")
    public AjaxResult getInfo(@PathVariable("uuid") String uuid) {
        return success(gatewayAppRouterService.selectGatewayAppRouterByUuid(uuid));
    }

    /**
     * 根据appid获取应用权限详细信息
     */
    @PreAuthorize("@ss.hasPermi('gatewayadmin:permission:query')")
    @GetMapping(value = "/ByAppId/{uuid}")
    public AjaxResult getInfoByAppId(@PathVariable("uuid") String uuid) {
        GatewayAppRouter gatewayAppRouter = new GatewayAppRouter();
        gatewayAppRouter.setAppId(uuid);
        List<GatewayAppRouter> gatewayAppRouters = gatewayAppRouterService.selectGatewayAppRouterList(gatewayAppRouter);
        return success(gatewayAppRouters);
    }


    /**
     * 新增应用权限
     */
    @PreAuthorize("@ss.hasPermi('gatewayadmin:permission:add')")
    @Log(title = "应用权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GatewayAppRouter gatewayAppRouter) {
        Integer rows = 0;
        //新增
        if (StringUtils.isBlank(gatewayAppRouter.getUuid())) {
            UUID uuid = UUID.randomUUID();
            gatewayAppRouter.setUuid(uuid.toString());
            rows = gatewayAppRouterService.insertGatewayAppRouter(gatewayAppRouter);
        } else {
            //修改
            rows = gatewayAppRouterService.updateGatewayAppRouter(gatewayAppRouter);
        }
        return toAjax(rows);
    }

    /**
     * 批量应用权限
     */
    @PreAuthorize("@ss.hasPermi('gatewayadmin:permission:add')")
    @Log(title = "应用权限", businessType = BusinessType.INSERT)
    @PostMapping("/patchAdd")
    public AjaxResult patchAdd(@RequestBody List<GatewayAppRouter> gatewayAppRouters) {
        Integer rows = 0;
        //新增
        for (GatewayAppRouter gatewayAppRouter : gatewayAppRouters) {
            // 处理是添加
            if (StringUtils.isBlank(gatewayAppRouter.getUuid())) {
                String uuid = UUID.randomUUID().toString();
                gatewayAppRouter.setUuid(uuid);
                rows = gatewayAppRouterService.insertGatewayAppRouter(gatewayAppRouter);
                // 删除
            } else if (StringUtils.isNotBlank(gatewayAppRouter.getUuid()) && gatewayAppRouter.getChecked().equals(false)) {
                rows = gatewayAppRouterService.deleteGatewayAppRouterByUuid(gatewayAppRouter.getUuid());
             //修改过期时间
            } else if (StringUtils.isNotBlank(gatewayAppRouter.getUuid()) && gatewayAppRouter.getChecked().equals(true) && Objects.nonNull(gatewayAppRouter.getExpiration())) {
                rows = gatewayAppRouterService.updateGatewayAppRouter(gatewayAppRouter);
            }
        }
        return toAjax(rows);
    }

    /**
     * 修改应用权限
     */
    @PreAuthorize("@ss.hasPermi('gatewayadmin:permission:edit')")
    @Log(title = "应用权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GatewayAppRouter gatewayAppRouter) {
        return toAjax(gatewayAppRouterService.updateGatewayAppRouter(gatewayAppRouter));
    }

    /**
     * 删除应用权限
     */
    @PreAuthorize("@ss.hasPermi('gatewayadmin:permission:remove')")
    @Log(title = "应用权限", businessType = BusinessType.DELETE)
    @DeleteMapping("/{uuids}")
    public AjaxResult remove(@PathVariable String[] uuids) {
        return toAjax(gatewayAppRouterService.deleteGatewayAppRouterByUuids(uuids));
    }
}
